(function(){
    // 性能监控标记
    const startTime = Date.now();
    let isProcessing = false;

    // 缓存高频查询节点
    const body = document.body;
    const head = document.head;

    // 预定义样式常量（减少重复计算）
    const CONTAINER_STYLE = `
        position:fixed!important;
        top:0!important;
        left:0!important;
        width:100vw!important;
        height:100vh!important;
        z-index:2147483647!important;
        background:#000!important;
        transform:translateZ(0);
        will-change:transform;
    `;
    
    const VIDEO_STYLE = `
        width:100%!important;
        height:100%!important;
        object-fit:fill!important;
        position:absolute!important;
        top:50%!important;
        left:50%!important;
        transform:translate(-50%,-50%)!important;
        -webkit-transform-style:preserve-3d;
    `;

    // Shadow DOM查询优化（限制递归深度）
    function findVideoInShadow(root, depth=0) {
        if(depth > 3) return null; // 限制递归层级
        
        const video = root.querySelector('video');
        if(video) return video;

        const shadows = root.querySelectorAll('*').filter(n => n.shadowRoot);
        for(const node of shadows) {
            const result = findVideoInShadow(node.shadowRoot, depth+1);
            if(result) return result;
        }
        return null;
    }

    // 批量样式操作（减少重绘）
    function optimizeVideoElement(video) {
        video.style.cssText = VIDEO_STYLE;
        video.setAttribute('playsinline', '');
        video.setAttribute('webkit-playsinline', '');
        video.removeAttribute('controls');
    }

    // 节流控制栏移除
    const removeControls = (() => {
        const processed = new WeakSet();
        return function() {
            const selectors = [
                '#control_bar', '.controls', 
                '.vjs-control-bar', 'xg-controls',
                '.xgplayer-ads', '.fixed-layer',
                'div[style*="z-index: 9999"]'
            ];
            
            selectors.forEach(selector => {
                document.querySelectorAll(selector).forEach(e => {
                    if(processed.has(e)) return;
                    e.style.display = 'none';
                    e.parentNode?.removeChild(e);
                    processed.add(e);
                });
            });
        };
    })();

    // 硬件加速全屏容器
    function createVideoContainer() {
        const container = document.createElement('div');
        container.style.cssText = CONTAINER_STYLE;
        
        // 启用电视端GPU加速
        container.style.webkitBackfaceVisibility = 'hidden';
        container.style.perspective = '1000px';
        
        return container;
    }

    // 智能播放策略
    async function handleVideoPlay(video) {
        try {
            if(video.paused) {
                video.muted = true;
                await video.play();
                video.muted = false;
            }
        } catch(e) {
            video.muted = true;
            await video.play();
        }
    }

    // 优化检测逻辑
    function checkVideo() {
        if(isProcessing || (Date.now() - startTime > 20000)) {
            clearInterval(interval);
            return;
        }

        // 优先检测普通video
        let video = document.querySelector('video:not([data-processed])');
        if(!video) {
            const roots = [...document.querySelectorAll('*')]
                .filter(n => n.shadowRoot)
                .map(n => n.shadowRoot);
            
            for(const root of roots) {
                video = findVideoInShadow(root);
                if(video) break;
            }
        }

        if(video && video.readyState > 0) {
            isProcessing = true;
            video.dataset.processed = true;
            
            removeControls();
            optimizeVideoElement(video);
            
            const container = createVideoContainer();
            body.appendChild(container);
            container.appendChild(video);

            // 电视端全屏优化
            requestAnimationFrame(() => {
                video.webkitEnterFullscreen?.();
                handleVideoPlay(video);
            });

            clearInterval(interval);
        }
    }

    // 使用宽松的检测间隔（降低CPU压力）
    const interval = setInterval(checkVideo, 400);

    // 电视端不需要viewport meta
    const removeViewportMeta = () => {
        const meta = document.querySelector('meta[name="viewport"]');
        meta?.remove();
    };
    removeViewportMeta();
})();